package cn.edu.scau.nursingcenter.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cn.edu.scau.nursingcenter.bean.ResultBean;
import cn.edu.scau.nursingcenter.entiry.Bed_information;
import cn.edu.scau.nursingcenter.entiry.Stay_back;
import cn.edu.scau.nursingcenter.service.impl.Bed_informationServiceImpl;
import cn.edu.scau.nursingcenter.service.impl.Stay_backServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
/**
 * 退住信息管理控制器 Stay_backController
 * @author feng
 *
 */
@Api(tags = "退住信息管理控制器")
@RestController
@RequestMapping(value = "stay_back")
public class Stay_backController {
	@Autowired
	private Stay_backServiceImpl stay_backServiceImpl;
	@Autowired
	private Bed_informationServiceImpl bed_informationServiceImpl;
	
	/**
	 * 显示所有退住信息
	 * @return
	 */
	@ApiOperation(value = "显示所有退住记录信息")
	@ResponseBody
	@GetMapping(value = "/list")
	public ResultBean list() {
		ResultBean resultBean = null;
		List<Stay_back> stay_backs = stay_backServiceImpl.selectAll();
		if(stay_backs.size() != 0) {
			resultBean = new ResultBean(200,true,"显示成功",stay_backs);
		}else {
			resultBean = new ResultBean(5009,false,"显示失败",null);
		}
		return resultBean;
	}
//	/**
//	 * 新增退住
//	 * @return
//	 */
//	@ResponseBody
//	@PostMapping(value = "/insert")
//	public ResultBean insert(@Validated @RequestBody Stay_back stay_back) {
//		ResultBean resultBean = null;
//		//判断是否已经退住
//		Integer now_id = stay_back.getC_id();
//		List<Stay_back> stay_backs = stay_backServiceImpl.selectAll();
//		for(Stay_back now_stayBack:stay_backs) {
//			if(now_stayBack.getC_id() == now_id) {
//				resultBean = new ResultBean(5008,false,"退住办理失败,该客户已经办理退住,请勿重复办理",null);
//				return resultBean;
//			}
//		}
//		Stay_back targetStay_back = stay_backServiceImpl.insert(stay_back);
//		if(targetStay_back != null) {
//			resultBean = new ResultBean(200,true,"退住办理成功",targetStay_back);
//		}else {
//			resultBean = new ResultBean(5008,false,"退住办理失败",null);
//		}
//		return resultBean;
//	}
	/**
	 * 根据名字查询
	 * @return
	 */
	@ApiOperation(value = "根据客户名字查询")
	@ApiImplicitParam(name = "name",value = "客户姓名",required = true, dataType = "String")
	@ResponseBody
	@GetMapping(value = "/find")
	public ResultBean findByName(@RequestParam(defaultValue = "")String name) {
		ResultBean resultBean = null;
		List<Stay_back> stay_backs = stay_backServiceImpl.findByName(name);
		if(stay_backs.size()!=0) {
			resultBean = new ResultBean(200,true,"查询成功",stay_backs);
		}else {
			resultBean = new ResultBean(5007,false,"查询失败",null);
		}
		return resultBean;
	}
	/**
	 * 删除
	 * @return
	 */
	@ApiOperation(value = "根据编号删除")
	@ApiImplicitParam(paramType = "path",name = "id",value = "编号",required = true,dataType = "int")
	@ResponseBody
	@DeleteMapping(value = "/delete/{id}")
	public ResultBean delete(@PathVariable Integer id) {
		ResultBean resultBean = null;
		if(id != null) {
			stay_backServiceImpl.delete(id);
			resultBean = new ResultBean(200,true,"删除成功",null);
		}else {
			resultBean = new ResultBean(5006,false,"删除失败",null);
		}
		return resultBean;
	}
//	/**
//	 * 修改
//	 * @param stay_back
//	 * @return
//	 */
//	@ResponseBody
//	@PostMapping(value = "/update")
//	public ResultBean update(@Validated @RequestBody Stay_back stay_back) {
//		ResultBean resultBean = null;
//		Stay_back targetStay_back = stay_backServiceImpl.insert(stay_back);
//		if(targetStay_back != null) {
//			resultBean = new ResultBean(200,true,"修改成功",targetStay_back);
//		}else {
//			resultBean = new ResultBean(5008,false,"修改失败",null);
//		}
//		return resultBean;
//	}
	/**
	 * 新增退住 或者更新退住信息
	 * @param stay_back
	 * @return
	 */
	@ApiOperation(value = "新增退住 或者更新退住信息")
	@ResponseBody
	@PostMapping(value = "/save")
	public ResultBean save(@Validated @RequestBody Stay_back stay_back) {
		ResultBean resultBean = null;
		//判断是插入还是更新 id 不存在 就是插入
		if(stay_back.getStay_back_id()==null) {
			//插入 返回200
			Integer now_id = stay_back.getC_id();
			List<Stay_back> stay_backs = stay_backServiceImpl.selectAll();
			for(Stay_back now_stayBack:stay_backs) {
				if(now_stayBack.getC_id() == now_id) {
					resultBean = new ResultBean(5008,false,"退住办理失败,该客户已经办理退住,请勿重复办理",null);
					return resultBean;
				}
			}
			Stay_back targetStay_back = stay_backServiceImpl.insert(stay_back);
			if(targetStay_back != null) {
				//办理退住成功后 把床位信息修改
				Integer cid = targetStay_back.getC_id();
				Bed_information targetBed_information = bed_informationServiceImpl.findByCid(cid);
				targetBed_information.setBed_status(false);
				targetBed_information.setC_id(null);
				targetBed_information.setC_name(null);
				Bed_information bed_information = bed_informationServiceImpl.update(targetBed_information);
				resultBean = new ResultBean(200,true,"退住办理成功",targetStay_back);
			}else {
				resultBean = new ResultBean(5008,false,"退住办理失败",null);
			}
		}else {
			//更新 返回 201
			Stay_back targetStay_back = stay_backServiceImpl.insert(stay_back);
			if(targetStay_back != null) {
				resultBean = new ResultBean(201,true,"修改成功",targetStay_back);
			}else {
				resultBean = new ResultBean(5008,false,"修改失败",null);
			}
		}
		
		return resultBean;
	}

}
